package dolf.window;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class AboutWindow extends Window {
	private static final long serialVersionUID = 1L;
	private static AboutWindow instance = new AboutWindow();
	
	public AboutWindow() {
		initialize(SystemMessagesHandler.getInstance().getMessages().getString("about"));
	}
	
	public static AboutWindow getInstance() {
		return instance;
	}
	
	public void draw() {
		getContentPane().removeAll();
		initialize(SystemMessagesHandler.getInstance().getMessages().getString("about"));
		
		JTabbedPane tabPane = new JTabbedPane();
		tabPane.setBorder(new EmptyBorder(0,0,10,0));
		
		JPanel aboutPane = new JPanel();
		aboutPane.add(new JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("dolf/gfx/window/about_small.png"), "about image")));
		
		JPanel authorsPane = new JPanel(new GridLayout(0,1));
		authorsPane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("coding")+":"));
		authorsPane.add(new JLabel("Stefan Eberlein"));
		//authorsPane.add(new JLabel("Ralf Hackner"));
		authorsPane.add(new JLabel("Franz Richter"));
		
		JPanel licensePane = new JPanel(new GridLayout(0,1));
		licensePane.add(new JLabel(SystemMessagesHandler.getInstance().getMessages().getString("license_info")), BorderLayout.CENTER);
		licensePane.add(new JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("dolf/gfx/window/gplv3.png"), "GPLv3 Logo")));
		
		
		JButton showLicenseButton = new JButton(SystemMessagesHandler.getInstance().getMessages().getString("show_license"));
		showLicenseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFrame licenseFrame = new JFrame(SystemMessagesHandler.getInstance().getMessages().getString("license"));
				
				JTextArea textPane = new JTextArea(15,50);
				
				JScrollPane scrollPane = new JScrollPane(textPane); 
				textPane.setEditable(false);
				
				try {
					BufferedReader in = new BufferedReader(new InputStreamReader(
					this.getClass().getClassLoader().getResourceAsStream("dolf/LICENSE")));
					String line = "";
					while ( (line = in.readLine()) != null) {
						textPane.append(line+"\n");
					}
					textPane.setCaretPosition(0);
					
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				
				licenseFrame.add(scrollPane);
				
				licenseFrame.pack();
				licenseFrame.setLocationRelativeTo(instance);
				licenseFrame.setVisible(true);
				
				
			}
		});
		licensePane.add(showLicenseButton);
						
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("about"), aboutPane);
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("authors"), authorsPane);
		tabPane.addTab(SystemMessagesHandler.getInstance().getMessages().getString("license"), licensePane);
		
		JButton closeButton = new JButton(SystemMessagesHandler.getInstance().getMessages().getString("close"));
		closeButton.setMnemonic('c');
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		JPanel buttonPane = new JPanel();
		buttonPane.setVisible(true);
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
						
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(closeButton,BorderLayout.LINE_END);
		buttonPane.add(closeButton,BorderLayout.LINE_END);
		buttonPane.add(closeButton,BorderLayout.LINE_END);
		
		add(tabPane,BorderLayout.PAGE_START);
		add(new JSeparator());
		add(buttonPane, BorderLayout.PAGE_END);
		
		pack();
		setLocationRelativeTo(MainWindow.getInstance());
		setVisible(true);
	}
}
